package com.lzq.model.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : modify
 * @version : v1.0
 * @description :
 * @createTime : 2022/11/29 10:29
 * @updateUser : modify
 * @updateTime : 2022/11/29 10:29
 * @updateRemark : 迭代器模式
 */
public class StudentIteratorImpl implements StudentIterator {
    private List<Student> list;
    private int position = 0;

    public StudentIteratorImpl(List<Student> list) {
        this.list = list;
    }

    @Override
    public boolean hasNext() {
        return position < list.size();
    }

    @Override
    public Student next() {
        Student currentStudent = list.get(position);
        position++;
        return currentStudent;
    }
}

class Student {
    private String no;
    private String name;

    public Student(String no, String name) {
        this.no = no;
        this.name = name;
    }

    public String getNo() {
        return no;
    }

    public String getName() {
        return name;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "no='" + no + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

interface StudentIterator {
    /**
     * 是否有下一个节点
     *
     * @return boolean
     */
    boolean hasNext();

    /**
     * 得到下一个节点
     *
     * @return Student
     */
    Student next();
}

interface StudentAggregate {
    /**
     * 添加
     *
     * @param student
     */
    void addStudent(Student student);

    /**
     * 删除
     *
     * @param student
     */
    void removeStudent(Student student);

    /**
     * 获取迭代器
     *
     * @return StudentIterator
     */
    StudentIterator getStudentIterator();
}

class StudentAggregateImpl implements StudentAggregate {
    private List<Student> list = new ArrayList<Student>();

    @Override
    public void addStudent(Student student) {
        list.add(student);
    }

    @Override
    public void removeStudent(Student student) {
        list.remove(student);
    }

    @Override
    public StudentIterator getStudentIterator() {
        return new StudentIteratorImpl(list);
    }
}

class Test {
    public static void main(String[] args) {
        StudentAggregateImpl studentAggregate = new StudentAggregateImpl();
        studentAggregate.addStudent(new Student("1", "月月"));
        studentAggregate.addStudent(new Student("2", "黄月月"));
        studentAggregate.addStudent(new Student("3", "黄月"));
        studentAggregate.addStudent(new Student("4", "黄月月鸟"));
        StudentIterator iterator = studentAggregate.getStudentIterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            System.out.println(student.toString());
        }
    }
}